home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / bbsutil / bsrc_250.zip / XMREC.C < prev    next >
C/C++ Source or Header  |  1991-09-15  |  37KB  |  1,249 lines

  1. /*--------------------------------------------------------------------------*/
  2. /*                                                                          */
  3. /*                                                                          */
  4. /*      ------------         Bit-Bucket Software, Co.                       */
  5. /*      \ 10001101 /         Writers and Distributors of                    */
  6. /*       \ 011110 /          Freely Available<tm> Software.                 */
  7. /*        \ 1011 /                                                          */
  8. /*         ------                                                           */
  9. /*                                                                          */
  10. /*  (C) Copyright 1987-91, Bit Bucket Software Co., a Delaware Corporation. */
  11. /*                                                                          */
  12. /*                                                                          */
  13. /*                  This module was written by Bob Hartman                  */
  14. /*                                                                          */
  15. /*                                                                          */
  16. /*                BinkleyTerm Xmodem Receiver State Machine                 */
  17. /*                                                                          */
  18. /*                                                                          */
  19. /*    For complete  details  of the licensing restrictions, please refer    */
  20. /*    to the License  agreement,  which  is published in its entirety in    */
  21. /*    the MAKEFILE and BT.C, and also contained in the file LICENSE.250.    */
  22. /*                                                                          */
  23. /*    USE  OF THIS FILE IS SUBJECT TO THE  RESTRICTIONS CONTAINED IN THE    */
  24. /*    BINKLEYTERM  LICENSING  AGREEMENT.  IF YOU DO NOT FIND THE TEXT OF    */
  25. /*    THIS  AGREEMENT IN ANY OF THE  AFOREMENTIONED FILES,  OR IF YOU DO    */
  26. /*    NOT HAVE THESE FILES,  YOU  SHOULD  IMMEDIATELY CONTACT BIT BUCKET    */
  27. /*    SOFTWARE CO.  AT ONE OF THE  ADDRESSES  LISTED BELOW.  IN NO EVENT    */
  28. /*    SHOULD YOU  PROCEED TO USE THIS FILE  WITHOUT HAVING  ACCEPTED THE    */
  29. /*    TERMS  OF  THE  BINKLEYTERM  LICENSING  AGREEMENT,  OR  SUCH OTHER    */
  30. /*    AGREEMENT AS YOU ARE ABLE TO REACH WITH BIT BUCKET SOFTWARE, CO.      */
  31. /*                                                                          */
  32. /*                                                                          */
  33. /* You can contact Bit Bucket Software Co. at any one of the following      */
  34. /* addresses:                                                               */
  35. /*                                                                          */
  36. /* Bit Bucket Software Co.        FidoNet  1:104/501, 1:343/491             */
  37. /* P.O. Box 460398                AlterNet 7:491/0                          */
  38. /* Aurora, CO 80046               BBS-Net  86:2030/1                        */
  39. /*                                Internet f491.n343.z1.fidonet.org         */
  40. /*                                                                          */
  41. /* Please feel free to contact us at any time to share your comments about  */
  42. /* our software and/or licensing policies.                                  */
  43. /*                                                                          */
  44. /*--------------------------------------------------------------------------*/
  45.  
  46. /* Include this file before any other includes or defines! */
  47.  
  48. #include "includes.h"
  49.  
  50. void Find_Char (int);
  51. int Header_in_data (unsigned char *);
  52. void Send_ACK (XMARGSP);
  53. void Send_NAK (XMARGSP);
  54. long Open_Xmodem_File (XMARGSP);
  55.  
  56. int XRInit (XMARGSP, int);
  57. int XREnd (XMARGSP, int);
  58. int XRRecInit (XMARGSP);
  59. int XRBrecInit (XMARGSP);
  60. int XRRecStart (XMARGSP);
  61. int XRWaitFirst (XMARGSP);
  62. int XRWaitBlock (XMARGSP);
  63. int XRRestart (XMARGSP);
  64. int XRSetOvrdr (XMARGSP);
  65.  
  66. STATES Xmodem_Receiver[] = {
  67.    { "XRInit",  XRInit },
  68.    { "XREnd",  XREnd },
  69.    { "XR0",  XRRecInit },
  70.    { "XR0B", XRBrecInit },
  71.    { "XR1",  XRRecStart },
  72.    { "XR2",  XRWaitFirst },
  73.    { "XR3",  XRWaitBlock },
  74.    { "XR4",  XRRestart },
  75.    { "XR5",  XRSetOvrdr }
  76. };
  77.  
  78. long Open_Xmodem_File (XMARGSP args)
  79. {
  80.    char *s1, *s2;
  81.  
  82.    if (args->file_pointer == NULL)
  83.       {
  84.       args->temp_name = calloc (1, 80);
  85.  
  86.       if (args->path != NULL)
  87.          (void) strcpy (args->temp_name, args->path);
  88.  
  89.       s1 = &(args->temp_name[strlen (args->temp_name)]);
  90.       (void) strcpy (s1, "BTXXXXXX");
  91.       s2 = mktemp (args->temp_name);
  92.       if ((s2 == NULL) || ((args->file_pointer = fopen (s2, "wb")) == NULL))
  93.          {
  94.          status_line (MSG_TXT(M_TEMP_NOT_OPEN), s2);
  95.          return (-1L);
  96.          }
  97.       }
  98.  
  99.    throughput (0, 0L);
  100.    return (0L);
  101. }
  102.  
  103. long Set_Up_Restart (XMARGSP args)
  104. {
  105.    char foo[100];
  106.    char foo1[50];
  107.    struct stat st;
  108.  
  109.    args->sub_results = 0;
  110.  
  111.    /* Look for file in directory */
  112.    if (args->path != NULL)
  113.       (void) strcpy (foo, args->path);
  114.  
  115.    if ((args->received_name != NULL) &&
  116.        (strlen (args->received_name) > 0) &&
  117.         args->options.Resync)
  118.       {
  119.       (void) strcat (foo, args->received_name);
  120.  
  121.       if (stat (foo, &st) == 0)
  122.          {
  123.          if ((st.st_size == args->filelen) && (st.st_atime == (time_t)(args->save_filetime.oneword.timedate)))
  124.             {
  125.             if ((args->file_pointer = fopen (foo, "rb+")) != NULL)
  126.                {
  127.                throughput (0, 0L);
  128.                (void) fseek (args->file_pointer, 0L, SEEK_END);
  129.                args->sub_results = DID_RESYNC;
  130.                args->temp_name = calloc (1, 80);
  131.                (void) strcpy (args->temp_name, foo);
  132.                args->prev_bytes = args->filelen;
  133.                status_line (MSG_TXT(M_ALREADY_HAVE), foo);
  134.                status_line (MSG_TXT(M_SYNCHRONIZING_EOF));
  135.                return (args->total_blocks + 1L);
  136.                }
  137.             }
  138.          }
  139.  
  140.       /* Look for file in .Z file */
  141.       if (dexists (Abortlog_name))
  142.          {
  143.          (void) sprintf (Resume_info, "%ld %lo", args->filelen, args->save_filetime.oneword.timedate);
  144.          if (check_failed (Abortlog_name, args->received_name, Resume_info, foo1))
  145.             {
  146.             foo[0] = '\0';
  147.             /* Here it looks like it was a failed WaZOO session */
  148.             if (args->path != NULL)
  149.                (void) strcpy (foo, args->path);
  150.             (void) strcat (foo, foo1);
  151.  
  152.             if ((args->file_pointer = fopen (foo, "rb+")) != NULL)
  153.                {
  154.                (void) stat (foo, &st);
  155.                throughput (0, 0L);
  156.                args->temp_name = calloc(1, 80);
  157.                (void) strcpy (args->temp_name, foo);
  158.                args->prev_bytes = (st.st_size / 128L) * 128L;
  159.                (void) fseek (args->file_pointer, args->prev_bytes, SEEK_SET);
  160.                status_line (MSG_TXT(M_SYNCHRONIZING_OFFSET), args->prev_bytes);
  161.                return (args->prev_bytes / 128L + 1L);
  162.                }
  163.             }
  164.          }
  165.       }
  166.  
  167.    return (Open_Xmodem_File (args));
  168. }
  169.  
  170. void Finish_Xmodem_Receive (XMARGSP args)
  171. {
  172.    struct stat st;
  173.    char new_name[80];
  174.    struct utimbuf times;
  175.    int i, j, k;
  176.  
  177.    /* Set the file's time and date stamp */
  178.    if ((args->save_header == SOH) || (args->save_header == SYN))
  179.       {
  180.       (void) fclose (args->file_pointer);
  181.       times.modtime = times.UT_ACTIME = (time_t) args->save_filetime.oneword.timedate;
  182.       (void) utime (args->temp_name, (UTIMBUF *)×);
  183.       }
  184.    else
  185.       {
  186.       (void) strcpy (args->received_name, "");
  187.       (void) fclose (args->file_pointer);
  188.       }
  189.  
  190.    if (args->result == SUCCESS)
  191.       {
  192.       /* Get the file information */
  193.       (void) stat (args->temp_name, &st);
  194.  
  195.       throughput (1, (unsigned long) (st.st_size - args->prev_bytes));
  196.  
  197.       update_files (0);
  198.  
  199.       if (args->sub_results & DID_RESYNC)
  200.          {
  201.          status_line ("%s: %s", MSG_TXT(M_FILE_RECEIVED), args->temp_name);
  202.          }
  203.